package controll

import (
	"SalesMangent/function"
	"SalesMangent/pkg/app"
	"SalesMangent/pkg/e"

	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

func GetClient(c *gin.Context) {
	page := -1
	if arg := c.Query("page"); arg != "" {
		page = com.StrTo(arg).MustInt()
	}
	limit := -1
	if arg := c.Query("limit"); arg != "" {
		limit = com.StrTo(arg).MustInt()
	}
	searchName := ""
	if arg := c.Query("searchName"); arg != "" {
		searchName = arg
	}
	clientParam := map[string]interface{}{
		"page":       page,
		"limit":      limit,
		"searchName": searchName,
	}
	err, info, total := function.GetAllClient(clientParam)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total}, "查询成功")
}

func DelClient(c *gin.Context) {
	code := c.PostForm("c_code")
	err := function.DelClient(code)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "删除成功")
}

func AddClient(c *gin.Context) {
	err, info := function.AddClient(c)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, info, "增加成功")
}

func UpdateClient(c *gin.Context) {
	err := function.UpdateClient(c)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "更新成功")
}
